<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="test"></div>
  
  <!-- 引入react核心库 -->
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <!-- 用于支持react操作DOM -->
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <!-- 用于将jsx转为js -->
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  
  <script type="text/babel">
    class Life extends React.Component{

      state={opacity:1};

      death=()=>{
        //卸载组件
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      //组件挂载完毕
      componentDidMount(){
        this.timer=setInterval(()=>{
          //获取原状态
          let {opacity}=this.state;
          //修改状态
          opacity-=0.1;
          if(opacity<=0) opacity=1;
          //设置新的状态
          this.setState({opacity})
        },200)
      }

      //组件将要卸载
      componentWillUnmount(){
        //清除定时器
        clearInterval(this.timer)
      }

      //初始化渲染和状态更新后
      render(){
        console.log('render');
        return (
          <div>
            <h1 style={{opacity:this.state.opacity}}>React学不会怎么办？</h1>
            <button onClick={this.death}>不活了</button>
          </div>
        )
      }
    }

    ReactDOM.render(<Life/>,document.getElementById('test'))
  </script>
</body>
</html>